//
//  ViewController.m
//  JWCustomLeftTabBar
//
//  Created by sunjiawei on 2022/12/26.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong, readwrite) JWLeftTabBar *leftTabBar;
@property (nonatomic, strong, readwrite) UITabBarController *tabBarController;
/// 分割线
@property (nonatomic, strong) UIView *lineView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // UI
    [self setupSubviews];
    
    [self setupTabbarController];
}

/// 设置子视图
- (void)setupSubviews {
    
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.tabBar.hidden = YES;
    self.tabBarController.view.frame = self.view.bounds;

    [self.view addSubview:self.leftTabBar];
    [self addChildViewController:self.tabBarController];
    [self.view addSubview:self.tabBarController.view];
    [self.view addSubview:self.lineView];

    [self.leftTabBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.mas_equalTo(0);
        make.width.mas_equalTo(70);
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftTabBar.mas_right);
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(1);
    }];
    [self.tabBarController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.mas_equalTo(0);
        make.left.equalTo(self.leftTabBar.mas_right);
    }];
}

/// 根据index设置选中模块
/// - Parameter index: 位置
- (BOOL)setSelectedViewControllerAtIndex:(NSInteger)index {
    
    // 是否可以跳转
    if ([self leftTabbar:self.leftTabBar shouldSelectWithIndex:index]) {
        // 可以
        [self.leftTabBar setSelectItemWithIndex:index];
        [self leftTabbar:self.leftTabBar didSelectWithIndex:index];
        return YES;
    } else {
        return NO;
    }
}

#pragma mark - UI
- (void)setupTabbarController {
    
    // 清空
    [self.leftTabBar removeAllItems];
    self.tabBarController.viewControllers = @[];
    // 默认选中位置
    NSInteger selectedIndex = 0;
    // 隐藏
    self.leftTabBar.hidden = NO;
    self.tabBarController.view.hidden = NO;
    
    // 添加模块
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:[UIViewController new]];
    nav1.viewControllers.firstObject.view.backgroundColor = UIColor.redColor;
    [self.leftTabBar addItemWithTitle:@"消息1" image:@"tabbar_1_nor" selectedImage:@"tabbar_1_sel" gif:@"tabbar_1"];

    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:[UIViewController new]];
    nav2.viewControllers.firstObject.view.backgroundColor = UIColor.yellowColor;
    [self.leftTabBar addItemWithTitle:@"消息2" image:@"tabbar_1_nor" selectedImage:@"tabbar_1_sel" gif:@"tabbar_1"];
    
    UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:[UIViewController new]];
    nav3.viewControllers.firstObject.view.backgroundColor = UIColor.greenColor;
    [self.leftTabBar addItemWithTitle:@"消息3" image:@"tabbar_1_nor" selectedImage:@"tabbar_1_sel" gif:@"tabbar_1"];
    
    UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:[UIViewController new]];
    nav4.viewControllers.firstObject.view.backgroundColor = UIColor.orangeColor;
    [self.leftTabBar addItemWithTitle:@"消息4" image:@"tabbar_1_nor" selectedImage:@"tabbar_1_sel" gif:@"tabbar_1"];
    
    UINavigationController *nav5 = [[UINavigationController alloc] initWithRootViewController:[UIViewController new]];
    nav5.viewControllers.firstObject.view.backgroundColor = UIColor.grayColor;
    [self.leftTabBar addItemWithTitle:@"消息5" image:@"tabbar_1_nor" selectedImage:@"tabbar_1_sel" gif:@"tabbar_1"];
    
    self.tabBarController.viewControllers = @[nav1, nav2, nav3, nav4, nav5];
    [self.leftTabBar loadAllItems];
    // 设置选中
    [self.leftTabBar setSelectItemWithIndex:selectedIndex];
    self.tabBarController.selectedIndex = selectedIndex;
    
    self.tabBarController.view.hidden = NO;
}


#pragma mark - 状态栏显示
// 设置是否隐藏
- (BOOL)prefersStatusBarHidden {
    if (self.tabBarController.selectedViewController) {
        return self.tabBarController.selectedViewController.prefersStatusBarHidden;
    }
    return NO;
}

// 设置隐藏动画
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    if (self.tabBarController.selectedViewController) {
        return self.tabBarController.selectedViewController.preferredStatusBarUpdateAnimation;
    }
    return UIStatusBarAnimationNone;
}

#pragma mark - LeftTabBarDelegate
/// 是否可以选中
/// @param tabbar tabbar
/// @param index 位置
- (BOOL)leftTabbar:(JWLeftTabBar *)tabbar shouldSelectWithIndex:(NSInteger)index {
    return YES;
}
/// 选中后
/// @param tabbar tabbar
/// @param index 位置
- (void)leftTabbar:(JWLeftTabBar *)tabbar didSelectWithIndex:(NSInteger)index {

    if (self.tabBarController.selectedIndex != index) {
        // 未选中 设置选中
        [self.tabBarController setSelectedIndex:index];
    }
}

#pragma mark - get
- (JWLeftTabBar *)leftTabBar {
    if (!_leftTabBar) {
        _leftTabBar = [JWLeftTabBar new];
        _leftTabBar.delegate = self;
    }
    return _leftTabBar;
}
- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [UIView new];
        _lineView.backgroundColor = HexRGB(0xECECEC);
    }
    return _lineView;
}


@end
